<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Auction_coupon{
	var $id = 0;
	var $from = "";
	var $to = "";
	var $coins = 0;
	var $days_accept = 10;
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_AUCTION)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->from = $result[0]->from;
			$this->to = $result[0]->to;
			$this->coins = $result[0]->coins;
			$this->days_accept = $result[0]->days_accept;
			
			return $this;
		}
		return false;
	}
	
	function setFrom($input){ $this->from=$input; }
	function setTo($input){ $this->to=$input; }
	function setCoins($input){ $this->coins=$input; }
	function setDaysAccept($input){ $this->days_accept=$input; }
	
	function getId(){ return $this->id; }
	function getFrom(){ return $this->from; }
	function getTo(){ return $this->to; }
	function getCoins(){ return $this->coins; }
	function getDaysAccept(){ return $this->days_accept; }
	 
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('from', $this->from);
		$ci->db->set('to', $this->to);
		$ci->db->set('coins', $this->coins);
		$ci->db->set('days_accept', $this->days_accept);
		 
		$ci->db->where('id', $this->id)->update(TBL_AUCTION);
	}
	
	function createNewEntity(){
		$ci=& get_instance();
		
		$ci->db->set('days_accept', 10);
		$ci->db->insert(TBL_AUCTION);
		
		return $ci->db->insert_id();
	}
	
	function getList(){
		$ci=& get_instance();
		return $ci->db->get(TBL_AUCTION)->result();
	}
	
	function findObjBaseCredit($credit){
		$ci=& get_instance();
		$res = $ci->db->query("SELECT * FROM ".TBL_AUCTION." WHERE `from` <= $credit AND `to` >= $credit")->result();
		if(!$res) return false;
		return $res[0];
	}
	
	//endclass
}
